<template>
<view class="container"> 
  <form class="info_Form">
    <view class="info_image_container" :style="'width:' + sysWidth + 'px;height:' + sysWidth*0.45 + 'px'">
      <image :src="loginUser.platformUser.headimgurl" mode="aspectFill"></image><!--  bindtap='changeImage' -->
    </view>
    

    <view class="inputGroup" catchsubmit="changeUserInfo">
      

      <view class="input_item_container">
        <view class="input_item_container_title">门店名称</view>
        <input placeholder="门店名称" maxlength="10" name="nickname" :value="loginUser.platformUser.belongMendianName"></input>
      </view>
      
      <view class="input_item_container">
        <view class="input_item_container_title">手机号</view>
        <input placeholder="绑定手机号" maxlength="15" name="telno" :value="loginUser.telno"></input>
      </view>
      <view class="input_item_container">
        <view class="input_item_container_title">密码</view>
        <input placeholder="登录密码" maxlength="20" name="userTip" :value="loginUser.platformUser.userTip"></input>
      </view>
      

    </view> 
    <view class="CheckBox">
      <view class="checkBox_container">
        <view class="checkBoxName">性别</view>
        <radio-group name="sex" @change="sexChange">
          <radio name="sex1" value="1" :color="setting.platformSetting.defaultColor" :checked="loginUser.sex">男</radio>
          <radio name="sex1" value="0" :color="setting.platformSetting.defaultColor" :checked="loginUser.sex">女</radio>
        </radio-group>
      </view>
    </view>
    <button :loading="butn_show_loading" class="signIn" formType="submit" :style="'color:#fff;background:' + setting.platformSetting.defaultColor">保存门店信息</button>
   <!--  <button class='signIn' open-type='getUserInfo' bindtap='bindgetuserinfo' 
    style='color:{{setting.platformSetting.defaultColor}};background:#fff;border:1px solid {{setting.platformSetting.defaultColor}}'>获取授权信息</button> -->
  <!--   <button class='login_out' bindtap='loginOut'
     style='color:{{setting.platformSetting.defaultColor}};border:1px solid {{setting.platformSetting.defaultColor}}'>退出登录</button> -->
  </form>
</view>
</template>

<script>

export default {
  data() {
    return {
      loginUser: null,
      butn_show_loading: false,
      hasNoScope: false,
      canIUse: wx.canIUse('button.open-type.getUserInfo'),
      sysWidth: 320,
      userInfo: "",
      hasUserInfo: false,
      setting: ""
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      loginUser: getApp().globalData.loginUser,
      hasNoScope: getApp().globalData.hasNoScope,
      sysWidth: getApp().globalData.sysWidth
    });

    if (this.canIUse) {
      getApp().globalData.userInfoReadyCallback = res => {
        console.log(res);
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        });
      };
    }

    this.setData({
      setting: getApp().globalData.setting
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if (!getApp().globalData.checkIfLogin()) {
      return;
    }

    console.log(".............");
    console.log(this.loginUser);
    this.imageUrl = this.loginUser.platformUser.headimgurl;
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  methods: {
    bindgetuserinfo(e) {
      console.log(e);
    },

    /* 图片 */
    changeImage: function () {
      var that = this;
      wx.chooseImage({
        count: 1,
        // 默认9
        sizeType: ['original', 'compressed'],
        // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'],
        // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths[0];
          let imageParam = {};
          imageParam.imageUrl = tempFilePaths;
          var customIndex = getApp().globalData.AddClientUrl("/imageToBase64A.html", imageParam, 'post');
          /*wx.request({
            url: customIndex.url,
            data: customIndex.params,
            header: app.headerPost,
            method: 'POST',
            success: function (res) {
              console.log(res.data)
             
            },
            fail: function (res) {
              app.loadFail()
            },
            complete: function () {
              
            }
          })*/

          /*
          wx.uploadFile({
            url: '127.0.0.1:3000/chainalliance/imageToBase64A.html', //仅为示例，非真实的接口地址
            filePath: tempFilePaths,
            name: 'file',
            formData: {
              'user': 'test'
            },
            success: function (res) {
              var data = res.data
              console.log(res)
              //do something
            },
            fail:function(res){
              console.log(res)
            },
          })*/

          console.log(tempFilePaths); //console.log(that.data.loginUser)

          that.loginUser.userIcon = tempFilePaths;
          that.setData({
            loginUser: that.loginUser
          });
        }
      });
    },

    /* 提交 */
    //提交成功，重新登陆
    changeUserInfo: function (e) {
      console.log(e.detail);
      var reLoginData = {
        username: "",
        passworld: ""
      };
      this.setData({
        butn_show_loading: true
      });
      var loginUser = this.loginUser;
      reLoginData.username = loginUser.name;
      reLoginData.password = loginUser.passworld;
      var userInfo = e.detail.value; //检测手机号

      var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(19[0-9]{1})|(16[0-9]{1})|(14[0-9]{1}))+\d{8})$/;
      let phoneNo = userInfo.telno;

      if (!myreg.test(phoneNo)) {
        wx.showToast({
          title: "号码格式错误",
          image: '/wxcomponents/images/icons/tip.png',
          duration: 2000
        });
        this.setData({
          butn_show_loading: false
        });
        return;
      }

      userInfo.headimg = this.imageUrl;
      var that = this;
      var customIndex = getApp().globalData.AddClientUrl("/change_user_info.html", userInfo, 'post');
      wx.request({
        url: customIndex.url,
        data: customIndex.params,
        header: getApp().globalData.headerPost,
        method: 'POST',
        success: function (res) {
          console.log(res.data);

          if (res.data.errcode == '0') {
            wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 2000
            });
          } // that.loginOut()


          that.loginIn();
        },
        fail: function (res) {
          getApp().globalData.loadFail();
        },
        complete: function () {
          that.setData({
            butn_show_loading: false
          });
        }
      });
    },

    /* 退出登录 */
    loginOut: function () {
      return; // wx.showLoading({
      //   title: 'loading'
      // })

      getApp().globalData.showToastLoading('loading', true);
      wx.removeStorage({
        key: 'loginUser',
        success: function (res) {
          getApp().globalData.loginUser = null;
          console.log(res.data);
        },
        complete: function () {
          wx.hideLoading();
        }
      });
      wx.removeStorage({
        key: 'cookie',
        success: function (res) {
          getApp().globalData.cookie = null;
          getApp().globalData.header = {
            'content-type': 'application/json' // 默认值

          };
          getApp().globalData.headerPost = {
            'content-type': 'application/x-www-form-urlencoded'
          }, console.log(res.data);
          getApp().globalData.toIndex();
        },
        complete: function () {
          wx.hideLoading();
        }
      });
    },
    loginIn: function (data) {
      //app.wxLogin()
      getApp().globalData.get_session_userinfo(); // setTimeout(function () { wx.navigateBack() },200)

      return;
      console.log(data);
      var that = this;
      var loginUrl = getApp().globalData.AddClientUrl("Client.User.Login", data, 'post');
      wx.request({
        url: loginUrl.url,
        data: loginUrl.params,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res.header);
          var header = res.header;
          var cookie = null;

          if (!!header['Set-Cookie']) {
            cookie = header['Set-Cookie'];
          }

          if (!!header['set-cookie']) {
            cookie = header['set-cookie'];
          }

          console.log(cookie); //console.log(res.data)

          if (res.data.errcode == 0) {
            wx.setStorage({
              key: "cookie",
              data: cookie
            });
            getApp().globalData.header = {
              'content-type': 'application/json',
              // 默认值
              'Cookie': cookie
            };
            getApp().globalData.cookie = cookie;
            getApp().globalData.loginUser = res.data.relateObj;
            that.setData({
              loginUser: res.data.relateObj
            });
            wx.setStorage({
              key: "loginUser",
              data: res.data.relateObj
            });
            wx.navigateBack();
          } else {
            wx.showToast({
              title: '失败',
              icon: 'loading',
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log("fail");
          getApp().globalData.loadFail();
        }
      });
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>